/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.mzk.kramerius.articles.importFromLn.articlesData;

import cz.mzk.kramerius.articles.importFromLn.articlesData.errors.ACParsingError;
import cz.mzk.kramerius.articles.importFromLn.articlesData.errors.ArticleError;
import java.io.PrintStream;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author Martin Řehánek (rehan@mzk.cz)
 */
public class ArticleComponentsStatistics {

    private final Map<String, Integer> errorCounter = new HashMap<String, Integer>();
    private final Map<String, Integer> parsingErrorCounter = new HashMap<String, Integer>();
    private int correctComponentsCounter = 0;
    private int componentsCounter = 0;

    public void updateCounters(ArticleError error) {
        componentsCounter++;
        if (error == null) {
            correctComponentsCounter++;
        } else {
            if (error instanceof ACParsingError) {
                updateParsingCounters((ACParsingError) error);
            } else {
                updateErrorCounters(error);
            }

        }
    }

    private void updateErrorCounters(ArticleError error) {
        String className = error.getClass().getCanonicalName();
        Integer counter = errorCounter.get(className);
        if (counter == null) {
            counter = Integer.valueOf(0);
        }

        Integer newCounter = Integer.valueOf(counter.intValue() + 1);
        errorCounter.put(className, newCounter);
    }

    private void updateParsingCounters(ACParsingError error) {
        String field = error.getField();
        Integer counter = parsingErrorCounter.get(field);
        if (counter == null) {
            counter = Integer.valueOf(0);
        }
        Integer newCounter = Integer.valueOf(counter.intValue() + 1);
        parsingErrorCounter.put(field, newCounter);
    }

    private int getTotalParsingErrors() {
        int result = 0;
        for (String key : parsingErrorCounter.keySet()) {
            result += parsingErrorCounter.get(key).intValue();
        }
        return result;
    }

    public void print(PrintStream out) {
        out.println("Total article components: " + componentsCounter);
        out.println("Correct article components: " + correctComponentsCounter);
        int totalComponents = correctComponentsCounter;
        for (String className : errorCounter.keySet()) {
            out.println(className + ": " + errorCounter.get(className));
            totalComponents += errorCounter.get(className);
        }
        int totalParsingErrors = getTotalParsingErrors();
        totalComponents += totalParsingErrors;
        if (!parsingErrorCounter.isEmpty()) {
            out.println(ACParsingError.class.getCanonicalName() + ": " + totalParsingErrors + " (details follow)");
            for (String field : parsingErrorCounter.keySet()) {
                out.println(field + ": " + parsingErrorCounter.get(field));
            }
        }

        if (totalComponents != componentsCounter) {
            out.print("Error: componentsCounter(" + componentsCounter + ") != correctArticles + error sum (" + totalComponents + ")");
        }
    }
}
